<template>
  <el-container>
    <el-aside style="width:auto;">
      <div class="hideBtnDiv">
        <a href="javascript:void(0)" class="hideMenus" @click="isCollapse = !isCollapse">
          <i :class="isCollapse ? 'el-icon-arrow-right' : 'el-icon-arrow-left'"></i>
        </a>
      </div>
      <el-menu
        router
        :default-active="key"
        class="el-menu-vertical-demo"
        :collapse="isCollapse"
        background-color="#545c64"
        text-color="#fff"
        active-text-color="#ffd04b">
        <Sidebar :menus="menus"></Sidebar>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header>
        <div class="my-header">
          <div class="my-header-l">
            <div style="margin-right: 10px;">
              <i class="el-icon-s-unfold" style="font-size: 20px;"></i>
            </div>
            <div>
              <el-breadcrumb separator="/">
                <el-breadcrumb-item v-for="item in breadcrumb" :key="item.path">
                  {{$t('menu.' + item.code)}}
                </el-breadcrumb-item>
              </el-breadcrumb>
            </div>
          </div>
          <div class="my-header-r">
            <el-link class="exit-btn" @click="changeLanguage('zh')">中文</el-link>
            /
            <el-link class="exit-btn" @click="changeLanguage('en')">EN</el-link>
            &nbsp;&nbsp;
            <el-link class="exit-btn" @click="loginOut">{{$t('common.exit')}}</el-link>
          </div>
        </div>
      </el-header>
      <el-main>
        <el-tabs v-model="tabsModel" type="card" @tab-click="clickTabs" class="el-tabs" style="height: 100%;">
          <el-tab-pane v-for="(item,index) in tabList "  :key="item.code" :label="item.name" :name="item.path" style="height: 100%;">
            <section style="height: 100%;">
              <transition name="fade" mode="out-in">
                <router-view :key="key"></router-view>
              </transition>
            </section>
          </el-tab-pane>
        </el-tabs>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
  import Sidebar from './Sidebar';
  import {resetRouter} from '@/router/index';
  import {userInfo, putLocal} from '@/api/user/index';

  export default {
    name: "Main",
    components: {
      Sidebar
    },
    data() {
      return {
        isCollapse: false,
        breadcrumb: null,
        menus: this.$store.getters.menus,
        tabList:[{
          name:'用户中心',
          path:'/home',
          code:"center"
        }],
        activeTabsMenu:{},
        itemCheck:false,
        currentTabPath:'',
        tabCount:1,
        tabsModel:'/home'

      }
    },
    computed: {
      key() {
        return this.$route.path !== undefined ? this.$route.path : '/home';
      }
    },
    created() {
      this.getBreadcrumb();
    },
    methods: {
      loginOut() {
        this.$store.dispatch('loginOut').then(async () => {
          this.$router.matcher = resetRouter().matcher;
          await this.$router.push({path: '/login'});
        });
      },
      getBreadcrumb() {
        this.breadcrumb = [];
        let path = this.$route.fullPath;
        this.breadcrumb = this.$store.getters.breadcrumb[path];
      },
      changeLanguage(v) {
        if (this.$i18n.locale != v) {
          this.$i18n.locale = v;
          putLocal(v);
        }
      },searchTab(tabPath){

        this.tabList.forEach(currentClickMenuNode =>{
          console.log("to path:"+currentClickMenuNode);

          if(currentClickMenuNode.path.indexOf(tabPath) !== -1){
            //Tab 存在
            this.itemCheck = true;
          }
        });
        return this.itemCheck;

      },
      addTab(menuNode){

        this.tabCount = ++this.tabCount;
        this.tabList.push({
          name:menuNode.name,    //中文名称
          path:menuNode.path
        });
        console.log("insert node path :"+menuNode.path)
        this.tabsModel = menuNode.path;
      },
      clickTabs(tab,event){

        this.tabsModel =  tab.name;
        this.$router.push({path: tab.name});

      }
    },
    watch: {
      $route: function(currentClickMenuNode){
        //判断当前路径是否存在
        this.itemCheck = this.searchTab(currentClickMenuNode.path);
        //获得节点路径
        this.currentTabPath = currentClickMenuNode.path;

        if(this.itemCheck){
          console.log("show tab :"+currentClickMenuNode.path);
          this.tabsModel = currentClickMenuNode.path
        }else{
          console.log("add tab :"+currentClickMenuNode.path);
          this.addTab(currentClickMenuNode);
        }


        //同步面包屑

        let path = currentClickMenuNode.path;
        this.breadcrumb = this.$store.getters.breadcrumb[path];
      }
    }
  }
</script>

<style>
  .hideBtnDiv {
    text-align: right;
    background-color: #656d75;
  }

  .hideMenus {
    display: inline-block;
    line-height: 60px;
    width: 16px;
    color: #ffffff;
    background-color: #767f86;
  }

  .hideMenus:hover {
    background-color: #878f97;
  }

  .el-menu {
    border-right: none;
  }

  .el-menu-vertical-demo:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
  }

  .el-header, .el-footer {
    background-color: #E9EEF3;
    color: #333;
    padding: 0;
  }

  .el-aside {
    background-color: #545c64;
    color: #333;
  }

  .el-main {
    background-color: #FFFFFF;
    color: #333;
    padding: 0;
  }

  .el-container {
    height: 100%;
  }

  .my-header {
    display: flex;
    justify-content: space-between;
  }

  .my-header-l {
    display: flex;
    margin: 20px 10px auto 15px;
  }

  .my-header-r {
    margin: 20px 15px auto 10px;
  }

  .el-breadcrumb {
    line-height: normal;
  }

  .exit-btn {
    line-height: normal;
    margin-top: 0;
    vertical-align: top;
  }
  .el-tabs__content{
    height: calc(100% - 60px);
  }

  .el-table__placeholder{
    margin-right: 3px;
  }

  .el-dialog{
    display: flex;
    flex-direction: column;
    margin:0 !important;
    position:absolute;
    top:50%;
    left:50%;
    transform:translate(-50%,-50%);
  }

  .el-dialog .el-dialog__body{
    flex:1;
    overflow: auto;
  }
</style>
